package com.backstage.controller;

import com.backstage.common.annotation.log.Operate;
import com.backstage.common.base.bean.Message;
import com.backstage.common.base.enum_bean.MessageEnumStatus;
import com.backstage.common.base.enum_bean.MessageEnumTitle;
import com.backstage.common.base.validator.Add;
import com.backstage.common.base.validator.Other;
import com.backstage.common.page.Page;
import com.backstage.common.uitil.DES_3;
import com.backstage.common.uitil.JsonUtil;
import com.backstage.common.uitil.StringUtil;
import com.backstage.common.uitil.ValidatorUtils;
import com.backstage.entity.Role;
import com.backstage.entity.User;
import com.backstage.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:阮雪峰
 * @Date：2017/7/30 10:54
 * @pagekage: com.backstage.controller
 * @project: authority
 */
@RequestMapping("/user")
@Controller
public class UserController extends BaseController{

    @Resource(name = "userService")
    private UserService userService;

    /**
     * 分页查询用户
     * @param page
     * @param User
     * @return
     */
    @RequiresPermissions("sys:user:list")
    @RequestMapping(value = "/queryByPage", method = RequestMethod.POST)
    @ResponseBody
    public Page<User> queryByPage(Page<User> page, User User){
        userService.userByPage(page, User);
        return page;
    }

    /**
     * 新增
     * @param user
     * @return
     */
    @Operate("用户管理：新增")
    @RequiresPermissions("sys:user:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Message add(User user){
        Message message = ValidatorUtils.validateEntity(user, Add.class);
        if(message.getStatus() == MessageEnumStatus.FAIL){
            return message;
        }
        if(userService.isExistUserName(user)){
            return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败,  "用户名已存在");
        }
        user.setCreator(this.getLoginUser().getUid());
        userService.add(user);
        return Message.ADD_SUCCESS;
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @Operate("菜单管理：修改")
    @RequiresPermissions("sys:user:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Message edit(User user){
        userService.edit(user);
        return Message.EDIT_SUCCESS;
    }

    /**
     * 删除
     * @param users
     * @return
     */
    @Operate("菜单管理：删除")
    @RequiresPermissions("sys:user:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Message delete(ArrayList<User> users){
        userService.delete(users);
        return Message.DELETE_SUCCESS;
    }

    /**
     * 通过ID查询
     * @param user
     * @return
     */
    @RequestMapping(value = "/getUserById", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getUserById(User user) throws JsonProcessingException {
        return JsonUtil.objectToMap(userService.getUserById(user));
    }

    /**
     * 禁用或者取消禁用账户
     * @param user
     * @return
     */
    @Operate("菜单管理：取消或禁用账户")
    @RequiresPermissions("sys:user:updateStatus")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public Message updateStatus(User user){
        userService.updateStatus(user);
        return Message.OPERATE_SUCCESS;
    }

    /**
     * 用户名是否存在
     * @param user
     * @return
     */
    @RequestMapping(value = "/isExistUserName", method = RequestMethod.POST)
    @ResponseBody
    public Boolean isExistUserName(User user){
        return userService.isExistUserName(user);
    }

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping(value = "/getAllRoles", method = RequestMethod.POST)
    @ResponseBody
    public List<Role> getAllRoles(){
        return userService.getAllRoles();
    }

    /**
     * 修改密码
     * @param user
     * @return
     */
    @Operate("菜单管理：修改密码")
    @RequestMapping("/updatePassword")
    @ResponseBody
    public Message updatePassword(@Valid User user){
        Message message = ValidatorUtils.validateEntity(user, Other.class);
        if(MessageEnumStatus.FAIL == message.getStatus()){
            return message;
        }

        if (!user.getPassword().equals(user.getRePassword())){
            return Message.FAIL.setMessage("两次密码输入不相同");
        }

        User loginUser = this.getLoginUser();
        if(!user.getOldPassword().equals(DES_3.decryptBy3DES(loginUser.getPassword(), loginUser.getDes_key()))){
            return Message.FAIL.setMessage("旧密码不正确");
        }
        loginUser.setDes_key(StringUtil.getRandomStr(32));
        loginUser.setPassword(DES_3.encryptBy3DES(user.getPassword(), loginUser.getDes_key()));
        userService.updatePassword(loginUser);
        SecurityUtils.getSubject().logout();
        return Message.OPERATE_SUCCESS;
    }

    /**
     * 初始化密码
     * @param user
     * @return
     */
    @Operate("菜单管理：初始化密码")
    @RequestMapping(value = "/initPassword", method = RequestMethod.POST)
    @ResponseBody
    public Message initPassword(User user){
        userService.initPassword(user);
        return new Message(MessageEnumStatus.SUCCESS, MessageEnumTitle.成功, "密码初始化成功，下次登录时生效");
    }

}
